################################################################################
# A Vala module for CMake
################################################################################
# Copyright (C) 2012-2013 Fernando "Franz" Zapata
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
################################################################################
# This module sets the following variables:
#   VALA_FOUND      ... if the vala compiler (valac) was found
#   VALA_EXECUTABLE ... pathname of the valac program
#   VALA_VERSION    ... the version of the valac program found
################################################################################
# The vala_precompile function compiles vala to c for use in CMake.
#
# The first parameter is the name of the variable which will be set to the list
# of c files generated by compiling vala source code. This is followed by the
# list of input vala files.
################################################################################

find_program (VALA_EXECUTABLE valac)

if (VALA_EXECUTABLE)
	execute_process (
	COMMAND
		${VALA_EXECUTABLE}
		--version
	OUTPUT_VARIABLE
		VALA_VERSION
	)
	string (REGEX MATCH "[.0-9]+" VALA_VERSION ${VALA_VERSION})
endif ()

include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (Vala
REQUIRED_VARS
	VALA_EXECUTABLE
VERSION_VAR
	VALA_VERSION
)

mark_as_advanced (VALA_EXECUTABLE VALA_VERSION)

################################################################################

include (CMakeParseArguments)
function (vala_precompile output)
	cmake_parse_arguments (ARGS
		""
		"SOURCEDIR;OUTDIR;GENERATE_VAPI;GENERATE_HEADER;GENERATE_GIR;GENERATE_SYMBOLS"
		"PACKAGES;OPTIONS;CUSTOM_VAPIS"
		${ARGN}
	)

	if (ARGS_SOURCEDIR)
		set (SOURCEDIR ${ARGS_SOURCEDIR})
	else ()
		set (SOURCEDIR ${CMAKE_CURRENT_SOURCE_DIR})
	endif ()

	if (ARGS_OUTDIR)
		set (OUTDIR ${ARGS_OUTDIR})
	else ()
		set (OUTDIR ${CMAKE_CURRENT_BINARY_DIR})
	endif ()

	set (package_opts)
	foreach (pack ${ARGS_PACKAGES})
		list (APPEND package_opts --pkg=${pack})
	endforeach ()

	set (in_file_list)
	set (out_file_list)
	set (out_file_list_print)
	foreach (src ${ARGS_UNPARSED_ARGUMENTS})
		string (REGEX MATCH "^/" matched ${src})
		if (matched MATCHES "/")
			set (in_file ${src})
		else ()
			set (in_file ${CMAKE_CURRENT_SOURCE_DIR}/${src})
		endif ()
		get_filename_component (filename ${in_file} NAME)
		string (REPLACE ".vala" ".c" filename ${filename})
		string (REPLACE ".gs" ".c" filename ${filename})
		list (APPEND out_file_list_print ${filename})
		list (APPEND out_file_list ${OUTDIR}/${filename})
		list (APPEND in_file_list ${in_file})
	endforeach ()

	foreach (src ${ARGS_CUSTOM_VAPIS})
		string (REGEX MATCHE "^/" matched ${src})
		if (matched MATCHES "/")
			set (in_file ${src})
		else ()
			set (in_file ${CMAKE_CURRENT_SOURCE_DIR}/${src})
		endif ()
		list (APPEND in_file_list ${in_file})
	endforeach ()

	if (ARGS_GENERATE_VAPI)
		set (filename ${ARGS_GENERATE_VAPI}.vapi)
		list (APPEND out_file_list_print ${filename})
		list (APPEND out_file_list ${OUTDIR}/${filename})
		set (vapi_opts "--library=${ARGS_GENERATE_VAPI}" "--vapi=${filename}")
		if (NOT ARGS_GENERATE_HEADER)
			set (ARGS_GENERATE_HEADER ${ARGS_GENERATE_VAPI})
		endif ()
	endif ()

	if (ARGS_GENERATE_HEADER)
		set (filename ${ARGS_GENERATE_HEADER}.h)
		list (APPEND out_file_list_print ${filename})
		list (APPEND out_file_list ${OUTDIR}/${filename})
		set (header_opts "--header=${filename}")
	endif ()

	if (ARGS_GENERATE_GIR)
		set (filename ${ARGS_GENERATE_GIR}.gir)
		list (APPEND out_file_list_print ${filename})
		list (APPEND out_file_list ${OUTDIR}/${filename})
		set (gir_opts "--gir=${filename}")
	endif ()

	if (ARGS_GENERATE_SYMBOLS)
		set (filename ${ARGS_GENERATE_SYMBOLS}.symbols)
		list (APPEND out_file_list_print ${filename})
		list (APPEND out_file_list ${OUTDIR}/${filename})
		set (gir_opts "--symbols=${filename}")
	endif ()

	add_custom_command (
	OUTPUT
		${out_file_list}
	COMMAND
		${VALA_EXECUTABLE}
		-C
		-b ${SOURCEDIR}
		-d ${OUTDIR}
		${package_opts}
		${vapi_opts}
		${header_opts}
		${gir_opts}
		${symbols_opts}
		${ARGS_OPTIONS}
		${in_file_list}
	DEPENDS
		${in_file_list}
	COMMENT
		"Generating ${out_file_list_print}"
	VERBATIM
	)

	set (${output} ${out_file_list} PARENT_SCOPE)
endfunction ()
